import json
import time

import requests


class Ararms():
    def __init__(self, zhi):
        self.__dict__.update(zhi)
        super().__init__()
        self.session = requests.session()

    ##签收获取
    def login(self):
        self.my_logg.info(f'{self.name}智能网管平台正在查询任务...')
        cookies = {
            'accessToken': self.accessToken,
        }
        self.headers = {
            'Accept': 'application/json',
            'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
            'Connection': 'keep-alive',
            'Content-Type': 'application/json; charset=utf-8',
            'Referer': self.url + '/alarmOrderCount/alarmsHandle',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0',
            'accessToken': self.accessToken,
            'userName': 'bm9j',
        }
        self.params = {'offset': '0', 'limit': '40', 'alarmOrderId': '', 'system': 'ALL', 'module': 'ALL',
                       'alarmOrderInfo': '', 'alarmNextDealTag': '20', 'level': '', 'startTime': self.startTime,
                       'endTime': self.end_time, 'userId': '99999808'}
        response = self.session.get(url=self.url + '/alarmQuery1/sys/alarmOrder/listTaskQueryToTianyan',
                                    headers=self.headers, params=self.params, cookies=cookies, verify=False)
        if response.status_code == 200:
            response_cyt = response.json()['data']
            if not response_cyt == []:
                for response_in in reversed(response_cyt):
                    self.my_logg.info(response_in)
                    pid = {"pid": response_in['pid'], "id": response_in['taskId']}
                    self.taskInfoListHandle(pid, response_in)
            else:
                self.my_logg.info(f'{self.name}智能网管平台没有签收任务...')
        else:
            self.my_logg.info(f'{self.name}智能网管平台cookie可能失效')

    def taskInfoListHandle(self, pid, response_in):
        self.session.headers['Content-Type'] = 'application/json; charset=utf-8'
        response = self.session.post(url=self.url + '/alarmQuery1/sys/alarmOrder/taskInfoListHandle',
                                     data=json.dumps(pid), verify=False)
        if response.status_code == 200:
            self.response_sign = response.json()
            self.taskId = pid['id']
            self.alarmOrder = self.response_sign["alarmOrder"]
            self.alarms_sign(response_in)

    def alarms_sign(self, response_in):
        json_data = {
            'userId': '13260973816@wo.cn',  # "firstDisposeStaffId"
            'taskId': self.taskId,  # "pid":response
            'alarmOrderId': self.alarmOrder["alarmOrderId"],  # alarmOrderId
            'message': self.alarmOrder['message'],
            'opinion': '',
            'alarmNextDealTag': '21',
            'subPerson': '',
            'updateModule': '',
            'email': '13260973816@wo.cn',  # groupStaff
        }
        print(json_data)
        # response = self.session.post(url=self.url + '/alarmQuery1/sys/alarmOrder/completeTaskInterface',
        #                              data=json.dumps(json_data), verify=False)
        # if response.status_code == 200:
        #     self.dingding(response_in)
        #     self.my_logg.info(f'{self.name}智能网管平台签收成功', response.json())
        # else:
        #     self.notcie_s.get_signature(f'{self.name}智能网管平台签收失败')
        #     self.my_logg.info(f'{self.name}智能网管平台签收失败', response.text)

    # def dingding(self, data):
    #     alarmOrderInfo = data["alarmOrderInfo"]
    #     system = data["system"]
    #     beginTimeDate = data["beginTimeDate"]
    #     beginTimeDate_time = beginTimeDate.split(" ")[1][:5]
    #     tongzhi = f"时间:[{beginTimeDate_time}], 系统:[{system}]，发生一条告警，告警描述为:[{alarmOrderInfo}]，请处置！"
    #     can = ["短信下发", "DCN区离线话单ftp_端口探测alert", "离线话单网元ftp_端口探测alert", "通知"]
    #     hulu = '忽略'
    #     if any((match := can_data) in alarmOrderInfo for can_data in can):
    #         if hulu not in alarmOrderInfo:
    #             for i in range(3):
    #                 self.notcie_s.get_signature(tongzhi)
    #                 time.sleep(1)
    #     else:
    #         self.notcie_s.get_signature(tongzhi)
    #     self.notcie_s.get_signature(f'{self.name}智能网管平台签收成功')
